<?

class Services extends CI_Model {
//	function Services() {
//		parent::Model();
//	}




//	function getByRange($range, $type, $x, $y) {
//		if ($range == "" || !($x > 0 && $y > 0)) return false;
//		
//		$distsqbound = 1000000*$range*$range;		
//		$sql = "SELECT POW(datas.x-?,2)+POW(datas.y-?,2)";		
//		$sql .= "as distsq, datas.*, id.type_id ";
//		if ($type == '') $sql .= ", serv.name as service_name ";
//		$sql .= "from (ics_provider_data datas, ics_services_id id) ";
//		if ($type == '') $sql .= "LEFT JOIN ics_services as serv ON (id.type_id = serv.type_id) ";		
//		$sql .= "WHERE POW(datas.x-?,2)+POW(datas.y-?,2)  <= $distsqbound ";
//		if ($type != '' && strlen($type) > 16) $sql .= "AND id.type_id = ? ";
//
//		$sql .= "AND datas.pro_id = id.pro_id ";
//		$sql .= "ORDER by distsq ";
//		$this->writeLog($sql);
//		$q = ($type != '' && strlen($type) > 16) ? $this->db->query($sql,array($x,$y,$x,$y,$type)) : $this->db->query($sql,array($x,$y,$x,$y));
//		if ($q->num_rows() > 0) {
//			return $q->result();
//		}
//		return false;
//	}
	
//	function getByType($type, $x, $y) {
//		if ($type == "" || !($x > 0 && $y > 0)) return false;
//		$sql = "SELECT serv.name, datas.pro_id, datas.pro_name, datas.x, datas.y, datas.postal, "
//		. " POW(datas.x-?,2)+POW(datas.y-?,2) as distsq "
//		. "	from (ics_services serv, ics_provider_data datas, ics_services_id id) "
//		. " INNER JOIN ( select DISTINCT pro_id from ics_zone where type_id = ? ) Z on datas.pro_id = Z.pro_id "
//		. " where serv.type_id=id.type_id AND datas.pro_id = id.pro_id "
//		. " AND serv.is_zone = 'Y'"
//		. " AND serv.type_id = ?"
//		. " ORDER BY distsq ASC";
//		//. " AND serv.is_zone = 'Y' AND datas.is_zone = 'Y' "		
//		$q = $this->db->query($sql,array($x,$y,$type,$type));
//		if ($q->num_rows() > 0) {
//			return $q->result();
//		}
//		return false;		
//	}
//	
//	function getZone($service, $provNo) {
//		if ($service == "" && $provNo == "") return false;
//		$zTable = "gis_lyr_ics_".trim(strtolower($service));
//		if ($this->db->table_exists($zTable)) {
//			$sql = "select g.OBJECT_VECTOR zone from `$zTable` g, ics_zone z
//			LEFT JOIN ics_provider_data `data` ON (data.pro_id = z.pro_id)
//			where z.type_id = ? AND g.OBJECT_ID = z.object_id AND ";
//			if (is_numeric($provNo)) $sql .= "data.no = ?";
//			else $sql .= "z.pro_id = ?";
//			
//			$q = $this->db->query($sql,array($service,$provNo));
//			
//			if ($q->num_rows() > 0) {
//				return $q->result();
//			}
//		}
//		return false;		
//	}
//	
//	function getAllZone($service) {
//		if ($service == "" && $provNo == "") return false;
//		$zTable = "gis_lyr_ics_".trim(strtolower($service));
//		if ($this->db->table_exists($zTable)) {
//			$sql = "select g.OBJECT_VECTOR zone from ? g, ics_zone z
//			LEFT JOIN ics_provider_data `data` ON (data.pro_id = z.pro_id)
//			where z.type_id = ? AND g.OBJECT_ID = z.object_id";
//			/*if (is_numeric($provNo)) $sql .= "data.no = '".$provNo."'";
//			else $sql .= "data.pro_id = '".$provNo."'";*/
//	
//			$q = $this->db->query($sql,array($zTable,$service));
//			if ($q->num_rows() > 0) {
//				return $q->result();
//			}
//		}
//		return false;		
//	}
//	
//	function getDgpZones($id='', $service=-1) {
//		$this->load->library('gis/Gisutil');
//		//$sql = "select OBJECT_ID zone_id, OBJECT_NAME zone_name,OBJECT_MAP_AIC zone_desc, OBJECT_VECTOR zone_data from gis_lyr_dgp_zone "; //OBJECT_MAP change to OBJECT_MAP_AIC
//		/*$sql = "select OBJECT_ID zone_id, OBJECT_NAME zone_name, OBJECT_VECTOR zone_data from gis_lyr_ura_zone ";
//		if (strpos($id, ",")) $sql .= " where OBJECT_ID IN('" . str_replace(",", "','", $id) . "') ";
//		else if ($id > 0) $sql .= " where OBJECT_ID = '" . $id . "' ";
//		$sql .= " order by OBJECT_NAME";*/
//		$sql = "select `gis_lyr_ura_zone`.OBJECT_ID zone_id, `gis_lyr_ura_zone`.OBJECT_NAME zone_name, `gis_lyr_ura_zone`.OBJECT_VECTOR zone_data,";
//		$sql .= "CASE `related_parent`.`parent_id`";
//		$sql .= "WHEN 1 THEN 'central' ";
//		$sql .= "WHEN 2 THEN 'east' ";
//		$sql .= "WHEN 3 THEN 'northeast' ";
//		$sql .= "WHEN 4 THEN 'north' ";
//		$sql .= "WHEN 5 THEN 'west' ";
//		$sql .= "END as `parent_zone` ";
//		$sql .= "from gis_lyr_ura_zone ";
//		$sql .= "left join `related_parent` ON `related_parent`.`object_id` = `gis_lyr_ura_zone`.`object_id`";
//		if (strpos($id, ","))
//		{
//			$arr_id = explode(",",$id);
//			$long_id = null;
//			foreach($arr_id as $part_id)
//			{
//				$long_id .= $this->db->escape($part_id).",";
//			}
//			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//			$sql .= " where `gis_lyr_ura_zone`.OBJECT_ID IN($long_id) ";
//		} 
//		else if ($id > 0)
//		{
//		 	$sql .= " where `gis_lyr_ura_zone`.OBJECT_ID = ".$this->db->escape($id)." ";
//		}
//		$sql .= " order by `gis_lyr_ura_zone`.OBJECT_NAME";
//		
//		// bind sql query
//		$q = $this->db->query($sql);
//		
//		if ($q->num_rows() > 0) {
//			$data = array();
//			foreach ($q->result() as $result) {
//				if ((!$this->isProviderDgpZoneServiceEmpty($service, $result->zone_id) && $id == -1) 
//					|| !($id == -1)) {
//					$line = '';
//					$xyList = explode("\n", $result->zone_data);
//					for ($i=0, $c=count($xyList); $i<$c; $i++) {
//						if (trim($xyList[$i]) != '') {
//							$xy = explode(",", $xyList[$i]);
//							$line .= "new Vertex(" . $xy[0] . "," . trim($xy[1]) . ")";							
//						}
//						if ($i < $c-2) $line .= ",";
//					}
//					$result->mapAttr = $this->gisutil->get_center_level($result->zone_data, 700, 420);
//					$result->vertexs = $line;
//					array_push($data, $result);
//				}
//			}
//			return $data;
//		}
//		return false;
//	}
//	
//	function isProviderDgpZoneServiceEmpty($service, $dgpZone) {
//		//if ($service == "" || $dgpZone == "") return false;
//		//$sql = "SELECT serv.name, datas.pro_id, datas.pro_name, datas.x, datas.y, datas.postal from "
//		$sql = "SELECT datas.pro_id from "
//		. "	ics_services serv, ics_provider_data datas, ics_services_id id"
//		. " where id.pro_id = datas.pro_id AND serv.type_id=id.type_id ";		
//		if (strpos($service, ","))
//		{
//			$arr_id = explode(",",$service);
//			$long_id = null;
//			foreach($arr_id as $part_id)
//			{
//				$long_id .= $this->db->escape($part_id).",";
//			}
//			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//			$sql .= " AND serv.type_id IN ($long_id) ";
//		}
//		else if ($service && $service != -1 && $service != "")
//		{
//		 	$sql .= " AND serv.type_id = ".$this->db->escape($service)." ";
//		}
//		if ($dgpZone != -1 && $dgpZone != "")
//		{
//		 	$sql .= " AND datas.zone =  ".$this->db->escape($dgpZone)." ";
//		}
//		
//		$q = $this->db->query($sql);
//		
//		if ($q->num_rows() > 0) {
//			return false;
//		}
//		return true;
//	}
//	
//	function getByProvider($id=null, $servId=null, $zone=null, $dgpMode=false, $start=-1, $limit=10) {
//
//		$count = false;
//		//if ($id == "" && $servId == null) return false;		
//		$sql = "select data.no, data.pro_id, data.pro_name, data.block, data.street, data.building, 
//			data.unit, data.postal, data.x, data.y, data.telp, data.fax, serv.type_id, 
//			serv.name, serv.is_zone";
//		
//		if ($id != "" || $servId != "") $sql .= ", zone.id";
//		if ($zone != "") $sql .= ", data.zone as id_zone, szone.OBJECT_NAME zone_name ";
//		// ($count) $sql = "select count(data.no) as c ";
//		$sql .= " from ics_provider_data `data` "
//			. " LEFT JOIN ics_services_id `real` ON (real.pro_id = data.pro_id) "
//			. " LEFT JOIN ics_services serv ON (serv.type_id = real.type_id) ";		
//		if ($zone != "") $sql .= " LEFT JOIN gis_lyr_dgp_zone szone ON (szone.OBJECT_ID = data.zone) ";			
//		if ($id != "" || $servId != "") $sql .= " LEFT JOIN ics_zone zone ON (zone.type_id = real.type_id AND zone.pro_id = real.pro_id) ";
//
//		$sql .= " WHERE (`x` > 0 AND `y` > 0) ";
//
//		if (strpos($id, ","))
//		{
//			$arr_id = explode(",",$id);
//			$long_id = null;
//			foreach($arr_id as $part_id)
//			{
//				$long_id .= $this->db->escape($part_id).",";
//			}
//			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//		 	$sql .= " and data.pro_id IN ($long_id) ";
//		}
//		else if ($id != null)
//		{
//			$sql .= " and data.pro_id = ".$this->db->escape($id)." ";
//		} 
//		
//		if (strpos($servId, ","))
//		{
//			$arr_id = explode(",",$servId);
//			$long_id = null;
//			foreach($arr_id as $part_id)
//			{
//				$long_id .= $this->db->escape($part_id).",";
//			}
//			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//			$sql .= " and serv.type_id IN ($long_id) ";
//		} 
//		else if ($servId != null && $servId != -1)
//		{
//			$sql .= " AND serv.type_id = ".$this->db->escape($servId)." ";
//		} 
//		
//		if (strpos($zone, ","))
//		{
//			$arr_id = explode(",",$zone);
//			$long_id = null;
//			foreach($arr_id as $part_id)
//			{
//				$long_id .= $this->db->escape($part_id).",";
//			}
//			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//			$sql .= " AND data.zone IN ($long_id) ";
//		} 
//		else if ($zone > 0)
//		{
//			$sql .= " and data.zone = ".$this->db->escape($zone)." ";
//		} 
//		else if ($zone != "")
//		{
//			$sql .=" and szone.OBJECT_NAME != '' ";
//		} 
//
//		$oSql = " order by " . ($zone != '' ? "szone.OBJECT_NAME," : "") . "data.pro_name,serv.name ";		
//		if ($dgpMode) {		
//                        $oSql = " order by " . ($zone != '' ? "szone.OBJECT_NAME ASC, data.pro_name,"
//                        . ($id != "" || $servId != "" ? "zone.id," : "") : "") . "serv.name";
//		}
//		
//		$sql .= $oSql;
//
//                //$sql .= " order by " . ($zone != '' ? "szone.OBJECT_NAME ASC, data.pro_name,zone.id" : "serv.name");
//
//		if (!$count && $start > -1) $sql .= " LIMIT $start, $limit ";
//
//		//binding sql
//		if (strpos($id, ",")) $id = "'".str_replace(",", "','", $id)."'";
//		if (strpos($id, ",") || $id != null) $bind_proid = 1; 
//		
//		if (strpos($servId, ",")) $servId = "'".str_replace(",", "','", $servId)."'";
//		if (strpos($servId, ",") || ($servId != null && $servId != -1)) $bind_typeid = 1; 
//		
//		if (strpos($zone, ",")) $zone = "'".str_replace(",", "','", $zone)."'";
//		if ((strpos($zone, ",")) || ($zone > 0)) $bind_zone = 1; 
//
//		$q = $this->db->query($sql);	
//		
//		if ($q && $q->num_rows() > 0) {
//			if ($count) {
//				$res = $q->result();
//				return $res[0]->c;
//			}
//			$data = array(); $provNo = 0; $aColor = array(); $idxZone = -1; $zoneId = 0;
//			foreach ($q->result() as $result) {
//				$result->full_address = $this->formatFullAddress($result);
//				$result->bubble_address = $this->formatBubbleAddress($result);
//				$result->bubble_address = trim($result->bubble_address);
//				//echo $result->bubble_address."<br>";
//				//$result->color = "#".$this->getRandomColor();
//				$result->color = "#".$this->getColor($aColor);
//				array_push($aColor, $result->color);
//				/*
//				* Old Logic, too complicated
//				if (isset($zoneArea)) {
//					$rZ = $this->inZone(explode(",", $zoneArea), $result->x, $result->y);
//					if ($rZ != 1) {
//						array_push($data, $result);
//					}
//				} else {
//					array_push($data, $result);
//				}
//				if (!strpos($servId, ",")) {
//					 else if ($provNo != $result->no) {
//						array_push($data, $result);
//					} else if ($id == null && $result->type_id != '') {
//						array_push($data, $result);
//					}
//				} else if (isset($result->zone) && $result->zone != '') {
//					array_push($data, $result);
//				} 
//				$provNo = $result->no;
//				*/
//				
//				/*
//				 * Old Logic, split array into zone-zone
//				if ($id == null && $zone != "") {
//					if ($result->type_id != '') {
//						if ($zoneId != $result->id_zone) $idxZone++; 
//						if (!isset($data[$idxZone]) || !is_array($data[$idxZone])) {
//							$data[$idxZone] = array();
//						}
//						$zoneId = $result->id_zone;
//						array_push($data[$idxZone], $result);
//					}
//				} else {
//					array_push($data, $result);
//				}
//				*/
//				array_push($data, $result);
//			}
//			return $data;
//		}
//		return false;		
//	}
//	
//	function getProviderByPostal($postal) {
//		if ($postal != null) {
//			$sql = "SELECT x as x_addr, y as y_addr, block,street as streetname,unit,postal, pro_id from ics_provider_data WHERE postal = ? ";
//			$q = $this->db->query($sql,array($postal));
//			return $q->result();
//		}
//		return false;
//	}
//	
//	function getNeighbourhoodLink($proId) {
//		if ($proId != null) {
//			$sql = "SELECT * from ics_neighbourhood_link WHERE ";
//			if (strpos($proId, ",")) 
//			{
//				$arr_id = explode(",",$proId);
//				$long_id = null;
//				foreach($arr_id as $part_id)
//				{
//					$long_id .= $this->db->escape($part_id).",";
//				}
//				$long_id = substr($long_id, 0, (strlen($long_id)-1) );
//				$sql .= " pro_id IN ($long_id) ";
//			}
//			else if ($proId != null) 
//			{
//				$sql .= " pro_id = ".$this->db->escape($proId)." ";
//			}
//			$sql .= " and postal != '#'";
//			
//			$q = $this->db->query($sql);
//			return $q->result();			
//		}
//		return false;
//	}
//	
//	function getProvider($id='', $zone='', $getZone=false) {
//		$sql = "select * from ics_provider_data where 1";
//		if ($id > 0) $sql .= " and pro_id = ?";
//		if ($zone > 0) $sql .= " and zone = ?";
//		$sql .= " ORDER BY pro_name";
//		
//		
//		// bind sql query
//		if($id > 0 && $zone > 0) $q = $this->db->query($sql,array($id,$zone));
//		else if($id > 0) $q = $this->db->query($sql,array($id));
//		else if($zone > 0) $q = $this->db->query($sql,array($zone));
//		else $q = $this->db->query($sql);
//		
//		if ($q && $q->num_rows() > 0) {
//			if (!$getZone)
//				$res = $q->result();
//			else {
//				$d = array();
//				foreach ($q->result() as $r) {
//					$r->is_zone = $this->isHaveZone('', $r->pro_id);
//					array_push($d, $r);
//				}
//				$res = $d;
//			}
//			return $res;
//		}
//		return false;		
//	}
//	
//	function isProviderDgpZoneEmpty($zone) {
//		$sql = "select zone from ics_provider_data 
//			WHERE zone = ?";
//		$q = $this->db->query($sql,array($zone));
//		if ($q && $q->num_rows() > 0) {
//			return false;
//		}
//		return true;
//	}
//	
//	function getServiceWhichHaveZone() {
//		$sql = "select serv.*, zone.id from ics_services `serv` 
//			LEFT JOIN ics_zone `zone` ON (serv.type_id = zone.type_id) 
//			WHERE zone.id != '' GROUP BY serv.type_id ORDER BY serv.name";
//		$q = $this->db->query($sql);
//		if ($q && $q->num_rows() > 0) {
//			return $q->result();
//		}
//		return false;			
//	}	
//	
//	function getCenterLevel($userAddress, $providers, $w, $h) {
//		$this->load->library('gis/Gisutil');
//		$xyData = "";
//		if ($userAddress != false) {
//			$xyData .= $userAddress[0]->x_addr . "," . $userAddress[0]->y_addr . ",";
//		}
//		if ($providers != false) {
//			foreach ($providers as $prov) {
//				$xyData .= $prov->x . ",". $prov->y .",";
//			}
//		}
//		if ($xyData != "") {			
//			return $this->gisutil->get_center_level(substr($xyData, 0, strlen($xyData)-1), $w, $h);
//		}
//		return false;
//	}
//	
	
	function get($type = '') {
		$sql = "select * from ics_services ";
		if (strpos($type, ",")) 
		{
			$arr_id = explode(",",$type);
			$long_id = null;
			foreach($arr_id as $part_id)
			{
				$long_id .= $this->db->escape($part_id).",";
			}
			$long_id = substr($long_id, 0, (strlen($long_id)-1) );
			
			$sql .= " where type_id IN ($long_id) ";
		} 
		else if (trim($type) != '') 
		{
			$sql .= " where type_id = ".$this->db->escape($type)." ";			
		}
		if (!($type > 0)) $sql .= " order by name";

		$q = $this->db->query($sql);
		
		if ($q->num_rows() > 0) {
			return $q->result();
		}
		return false;		
	}	

	
//	function getZoneState($type, $proId, $x, $y) {
//		if ($type == "" || $proId == "") return false;
//		$resultType = $this->isHaveZone($type, $proId);
//		$this->writeLog("\nhave zone : ".$resultType);
//		if ($resultType) {
//			$resultWhichHaveZone = $this->isInZone($type, $proId, $x, $y);
//			if ($resultWhichHaveZone) {
//				$state = "Y";
//			} else {
//				$state = "N";
//			}
//		} else {
//			//dont have zone
//			$state = "NA";
//		}
//		return $state;
//	}
//	
//	function isHaveZone($type, $pro_id='') {
//		$roid = ($pro_id != '')?" and pro_id='".$pro_id."'":"";
//		$ypeid = ($type != '')?" and type_id='".$type."'":"";
//		$sql = "select * from ics_zone where 1 ".$ypeid.$roid;
//		$this->writeLog("\nsql : ".$sql);
//		
//		// bind sql query
//		if($pro_id != '' || $type != '') $q = $this->db->query($sql,array($pro_id,$type));
//		else if($pro_id != '') $q = $this->db->query($sql,array($pro_id));
//		else if($type != '') $q = $this->db->query($sql,array($type));
//		else $q = $this->db->query($sql);
//		
//		if ($q->num_rows() > 0) {
//			return true;
//		}
//		return false;
//	}
//	
//	function getZoneStateforReport($type, $proId, $x, $y) {
//		if ($type == "" || $proId == "") return false;
//		echo "<font color=red>1. checking zone from ics_zone</font><br />";
//		$resultType = $this->isHaveZoneforReaport($type, $proId);
//		$this->writeLog("\nhave zone : ".$resultType);
//		if ($resultType) {
//			echo "<font color=red>2.Jika memiliki zone lalu cek XY dari input apa di dalam zone itu</font><br />";
//			$resultWhichHaveZone = $this->isInZoneforReport($type, $proId, $x, $y);
//			if ($resultWhichHaveZone) {
//				echo "<font color=red>3.Jika XY ($x,$y) terletak pada zone lalu assign sebagai Y</font><br />";
//				$state = "Y";
//			} else {
//				echo "<font color=red>3.Jika XY ($x,$y) tidak terletak pada zone lalu assign sebagai N</font><br />";
//				$state = "N";
//			}
//		} else {
//			echo "<font color=red>2.Jika tidak memiliki zone lalu assign sebagai NA</font><br />";
//			//dont have zone
//			$state = "NA";
//		}
//		return $state;
//	}
//	
//	function isHaveZoneforReaport($type, $pro_id='') {
//		$roid = ($pro_id != '')?" and pro_id='".$pro_id."'":"";
//		$ypeid = ($type != '')?" and type_id='".$type."'":"";
//		$sql = "select * from ics_zone where 1 ".$ypeid.$roid;
//		$this->writeLog("\nsql : ".$sql);
//		echo "<font color=red>query :</font><br />";
//		$sql_query = str_replace("from","<br />from",$sql);
//		$sql_query = str_replace("where","<br />where",$sql_query);
//		echo "<font color=red>".$sql_query."</font><br />";
//		$q = $this->db->query($sql);
//		if ($q->num_rows() > 0) {
//			return true;
//		}
//		return false;
//	}
//	
//	function isInZoneforReport($type, $pro_id, $x, $y) {
//		if ($type == "" || $pro_id == "" || !($x > 0 && $y > 0)) return false;
//		$data = $this->getZone($type, $pro_id);
//		//echo $type . "- ".$pro_id . "count > " .count($data)."\n";
//		if ($data) {
//			foreach ($data as $row) {
//				echo "<font color=red>query :</font><br />";
//				$report_query = str_replace("from","<br />from",$this->db->last_query());
//				$report_query = str_replace("where","<br />where",$report_query);
//				echo "<font color=red>".$report_query."</font><br />";
//				echo "<font color=red>has zone :</font><br />";
//				echo "<font color=red>".nl2br($row->zone)."</font><br />";
//				$zoneClient = $this->parseXy($row->zone);
//				$inZone = $this->ptInZone($zoneClient, $x, $y);
//				if ($inZone != 1) {
//					return true;
//				}
//			}			
//		}
//		return false;
//	}	
//	
//	function isInZone($type, $pro_id, $x, $y) {
//		if ($type == "" || $pro_id == "" || !($x > 0 && $y > 0)) return false;
//		$data = $this->getZone($type, $pro_id);
//		//echo $type . "- ".$pro_id . "count > " .count($data)."\n";
//		if ($data) {
//			foreach ($data as $row) {
//				$zoneClient = $this->parseXy($row->zone);
//				$inZone = $this->ptInZone($zoneClient, $x, $y);
//				if ($inZone != 1) {
//					return true;
//				}
//			}			
//		}
//		return false;
//	}	
//	
//	function inZone($pointer, $x, $y) {
//		$out =1;
//		$sumz = count($pointer);
//		$j = $sumz - 2;
//	
//		for ($i=0; $i<$sumz-1; $i+=2) {
//			if ( (($pointer[$i+1]<=$y) && ($y<$pointer[$j+1])) 
//				|| (($pointer[$j+1]<=$y) && ($y<$pointer[$i+1]))) {
//				if ($x < 
//					($pointer[$j]-$pointer[$i])*($y-$pointer[$i+1])/
//						($pointer[$j+1]-$pointer[$i+1])+$pointer[$i])  {
//					$out = 1 - $out;
//				}
//			}
//			$j = $i;
//		}
//		return $out;
//	}
//
//    function parseCXy($data) {
//            $data = explode(",", $data);
//			$return = array(); $idx = 1;
//            for ($i=0, $c=count($data); $i<$c; $i+=2) {
//                    if (isset($data[$i]) && isset($data[$i+1])) {
//			$return[$idx]['x'] = $data[$i];
//			$return[$idx]['y'] = $data[$i+1];
//			$idx++;
//                    }
//            }
//	return $return;
//    }
//	
//	
//	function parseXy($data) {
//			//update by akhyar
//			//last array is empty so must be erased
//			$dataSource0 = explode("\r\n", $data);
//			unset($dataSource);
//			foreach($dataSource0 as $value)
//			{
//				if($value!="" && $value !="\r" && $value !="\r\n")
//				$dataSource[] = $value;
//			}                
//			//end
//			if (count($dataSource) < 5) {
//					return $this->parseCXy($data);
//			}
//			$c = 0;
//			$datas = array();
//			for ($i=0, $c=count($dataSource); $i<$c; $i++) {
//					$xy = explode(",", $dataSource[$i]);
//					$datas[$i+1]['x'] = $xy[0];
//					$datas[$i+1]['y'] = $xy[1];
//			}
//			return $datas;
//	}
//	
//	function ptInZone($polyzoneXY,$x,$y) // 0 for inzone and 1 for not inzone
//	{
//		$count = count($polyzoneXY);
//		$inzone = FALSE;
//		$notInZone = 1;
//		$j = $count-1;
//		for($i=1;$i<$count;$i++)
//		{
//			if(($polyzoneXY[$i]['y'] < $y && $polyzoneXY[$j]['y'] >= $y) || ($polyzoneXY[$j]['y']<$y && $polyzoneXY[$i]['y'] >= $y))
//			{
//				$x0 = $polyzoneXY[$i]['x']+($y-$polyzoneXY[$i]['y'])/($polyzoneXY[$j]['y']-$polyzoneXY[$i]['y'])*($polyzoneXY[$j]['x']-$polyzoneXY[$i]['x']);
//				if($x0<$x) 
//				{
//					if($inzone === TRUE) {$inzone = FALSE;$notInZone=1;}
//					elseif($inzone === FALSE) {$inzone = TRUE;$notInZone=0;}
//				}
//			}
//			$j = $i;
//		}
//		//return $inzone;
//		return $notInZone ;
//	}
//
//
//	
//    function getRandomColor() {
//    	$d = array(0, 3, 6, 9, 12, 15);
//        $p0 = $d[mt_rand(0,5)];
//        $p1 = $d[mt_rand(0,5)];
//        $p2 = $d[mt_rand(0,5)];
//        $data = dechex($p0).dechex($p1).dechex($p2);
//        return $data;
//    }	
//    
//    function getColor($arrColor=array()) {
//    	/*
//    	$color = array("IndianRed","LightCoral","DarkSalmon","Crimson","Red","FireBrick","DarkRed",
//			"HotPink","DeepPink","MediumVioletRed","PaleVioletRed",
//			"Coral","Tomato","OrangeRed","DarkOrange","Orange",
//			"Gold","Yellow","DarkKhaki",
//			"Plum","Violet","Orchid","Fuchsia","Magenta","MediumOrchid",
//			"MediumPurple","BlueViolet","DarkViolet","DarkOrchid","DarkMagenta","Purple","Indigo","SlateBlue","DarkSlateBlue",
//			"GreenYellow","Chartreuse","LawnGreen","Lime","LimeGreen","SeaGreen","ForestGreen","Green","DarkGreen","YellowGreen","OliveDrab","Olive","DarkOliveGreen",
//			"MediumAquamarine","DarkSeaGreen","DarkCyan","Teal",
//			"CadetBlue","SteelBlue","Blue","MediumBlue","DarkBlue","Navy","MidnightBlue",
//			"BurlyWood","Tan","RosyBrown","Goldenrod","DarkGoldenrod","Peru","Chocolate","SaddleBrown","Sienna","Brown","Maroon",
//			"DarkGray","Gray", "DimGray","LightSlateGray","SlateGray","DarkSlateGray","Black");
//		$color = array("000", "300", "600", "900", "C00", "F00", "006",
//			"306", "606", "906", "C06", "F06", "00C", "30C", "60C", "90C", 
//			"C0C", "F0C");
//		*/
//		$color = array("000","300","600","900","C00","F00","003","303","603","903","C03","F03",
//			"006","306","606","906","C06","F06","009","309","609","909","C09","F09",
//			"00C","30C","60C","90C","C0C","F0C","00F","30F","60F","90F","C0F","F0F",
//			"030","330","630","930","C30","F30","033","333","633","933","C33","F33",
//			"036","336","636","936","C36","F36","039","339","639","939","C39","F39",
//			"03C","33C","63C","93C","C3C","F3C","03F","33F","63F","93F","C3F","F3F",
//			"060","360","660","960","C60","F60","063","363","663","963","C63","F63",
//			"066","366","666","966","C66","F66","069","369","669","969","C69","F69",
//			"06C","36C","66C","96C","C6C","F6C","06F","36F","66F","96F","C6F","F6F",
//			"090","390","690","990","C90","F90","093","393","693","993","C93","F93",
//			"096","396","696","996","C96","F96","099","399","699","999","C99","F99",
//			"09C","39C","69C","99C","C9C","F9C","09F","39F","69F","99F","C9F","F9F",
//			"0C0","3C0","6C0","9C0","CC0","FC0","0C3","3C3","6C3","9C3","CC3","FC3");
//		if (is_array($arrColor) && count($arrColor) > 0) {
//			$result = array_diff($color, $arrColor);
//			$c = $result[mt_rand(0,count($result)-1)];
//		} else {
//			$c = $color[mt_rand(0,count($color)-1)];
//		}
//		return $c;		
//    }
//
//	function errMessage($str) {
//		$d = "<?xml version=\"1.0\" encoding=\"UTF-8\"? >";
//		$d .= "<ics>\n<error>".$str."</error>\n</ics>";
//		$this->output->set_header("Content-type: text/xml");
//		$this->output->set_output($d);		
//	}
//	
//	function xmlHeader($type, $range, $block, $building, $street, $postal) {
//		$content = "<?xml version=\"1.0\" encoding=\"UTF-8\"? >";
//		$service = $type != '' ? $this->get($type) : false;
//		$name = $service != false ? $service[0]->name : '';
//		$content .= "<ics>\n"
//			. "<servicetype>".$type."</servicetype>\n"
//			. "<servicename>".htmlentities($name, ENT_QUOTES)."</servicename>\n"
//			. "<userlocation>\n";
//		if ($postal != '') { 
//			$content .= "<postal>".$postal."</postal>"; 
//		} else if ($building != '') { 
//			$content .= "<building>".$building."</building>"; 
//		} else {
//			if ($block != '') $content .= "<block>".$block."</block>";
//			if ($street != '') $content .= "<street>".$street."</street>";
//		} 
//		$content .= "</userlocation>\n";
//		if ($range > 0) { 
//			$content .= "<range>".$range."</range>\n";
//		}
//		//$this->output->set_header("Content-type: text/xml");
//		//$this->output->append_output($content);		
//		header("Content-type: text/xml");
//		echo $content;
//	}
//	
//	function formatFullAddress($result) {
//		$tmpAddr = $result->block != "" ? "Blk ".$result->block." " : "";
//		$tmpAddr .= $result->street." ".($result->unit != "" ? trim($result->unit) : ""). " ";
//		$tmpAddr .= $result->building != "" ? trim($result->building) ." " : "";
//		$tmpAddr .= "Singapore ".trim($result->postal) . ($result->telp != "" || $result->fax != "" ? "<br>" : "");
//		$tmpAddr .= $result->telp != "" ? "Tel: ".trim($result->telp)." " : "";
//		$tmpAddr .= $result->fax != "" ? "Fax: ".trim($result->fax) : "";	
//		return $tmpAddr;
//	}
//
//	function formatBubbleAddress($result) {
//		$tmpAddr = $result->block != "" ? "Blk ".trim($result->block)." " : "";
//		$tmpAddr .= $result->street." ".($result->unit != "" ? "<br>".trim($result->unit) : ""). " ";
//		$tmpAddr .= $result->building != "" ? trim($result->building) ." " : "";
//		$tmpAddr .= "<br>Singapore ".trim($result->postal) . ($result->telp != "" || $result->fax != "" ? "<br>" : "");
//		$tmpAddr .= $result->telp != "" ? "Tel: ".trim($result->telp)." " : "";
//		$tmpAddr .= $result->fax != "" ? "Fax: ".trim($result->fax) : "";	
//		return $tmpAddr;
//	}
//	
//	function xmlFooter() {
//		//$this->output->append_output("</ics>");
//		echo "</ics>";
//		die();
//	}
//	
//	
//	function xmlTag($pro_id, $pro_name, $distance, $state, $postal, $allserv=false, $servid='', $servname='') {
//		$name = htmlentities($pro_name, ENT_QUOTES);
//		$sname = htmlentities($servname, ENT_QUOTES);
//		$content = "\n";
//		$content .= "<provider><id>".$pro_id."</id><name>".$name."</name>";
//		if ($allserv && $servid != '' && $servname != '') { 
//			$content .= "<servicetype>".$servid."</servicetype>";
//			$content .= "<servicename>".$sname."</servicename>";
//		} 
//		$content .= sprintf("<distance>%01.2f</distance>", $distance);
//		$content .= "<state>".$state."</state>";
//		$content .= "<postalcode>".$postal."</postalcode></provider>";
//		//$this->output->append_output(str_replace('><',">\n<",$content));
//		echo str_replace('><',">\n<",$content);
//	}	
//	
	
	// <editor-fold defaultstate="collapsed" desc="UNUSED">

//	function writeLog($content) {
//		/*
//		$filename = "/var/www/html/aic/log/log.txt";
//		if (is_writable($filename)) {
//		    if (!$handle = fopen($filename, 'a')) {
//		         echo "Cannot open file ($filename)";
//		         exit;
//		    }		
//		    // Write $somecontent to our opened file.
//		    if (fwrite($handle, $content) === FALSE) {
//		        echo "Cannot write to file ($filename)";
//		        exit;
//		    }
//		} else {
//			echo "The file $filename is not writable";
//		}
//		*/
//	}

	
	/*
	function ptInZone($pointerXY, $x, $y) {
		$result = 1;
		$count = count($pointerXY);
		$j = $count-1;
		for ($i=1; $i<=$count; $i++) {
			if (!isset($pointerXY[$i]['y']) || !isset($pointerXY[$j]['y'])) continue;
			if (($pointerXY[$i]['y'] <= $y && $y < $pointerXY[$j]['y']) ||
			($pointerXY[$j]['y'] <= $y && $y < $pointerXY[$i]['y'])) {
				$x0 = ($pointerXY[$j]['x'] - $pointerXY[$i]['x']) * ($y - $pointerXY[$i]['y']) /
				($pointerXY[$j]['y'] - $pointerXY[$i]['y']) + $pointerXY[$i]['x'];
				if ($x < $x0) {
					$result = 1 - $result;
				}
			}
			$j = $i;
		}
		return $result;
	}
	*/
	
	 // </editor-fold>

	
}











	/////////LUM IMPLEMENT, tar DL SC nya dl
//	public function search($term) {
//		$term = $this->db->escape($term);
//
//		$sql = "SELECT posts.*,
//			CASE WHEN posts.title REGEXP $term THEN 1 ELSE 0 END AS keyword_in_title,
//			MATCH ( posts.title, posts.body ) AGAINST ($term) AS mysql_score
//			FROM posts
//			WHERE ( posts.title REGEXP $term OR posts.body REGEXP $term )
//			ORDER BY keyword_in_title DESC, mysql_score DESC, posts.date DESC LIMIT 0, 10";
//
//		$res = $this->db->query($sql);  
//		$items = array();
//		if ($res->num_rows() > 0) {
//			$count = 0;
//			foreach ($res->result() as $item) {
//				foreach ($item as $key => $value) {
//					$items[$count][$key] = $value;
//				}
//				$count++;
//			}
//		} 
//		return $items;
//	}



